$(function() {
	// //修改密码
	// $("#xiugaipassword").click(function () {
	// 	$("#dlg").dialog("open").dialog("setTitle","修改密码");
	// 	$('#fm').form('clear');
	//
	// })



	// 初始化主题
	var initTheme = function(themeName) {
		if(themeName == null) {
			themeName = $('#themeCss').attr('href').split('/').pop().split('.css')[0];
			// 添加勾选状态
			$(".themeItem ul li").removeClass('themeActive');
			$('.themeItem ul li .' + themeName).parent().addClass('themeActive');
			return;
		}
		var themeUrl = $('#themeCss').attr('href').split('/');
		themeUrl.pop();
		$('#themeCss').after('<link rel="stylesheet" href="' + themeUrl.join('/') + '/' + themeName + '.css" id="themeCss">');
		$('#themeCss').remove();

		// 添加勾选状态
		$(".themeItem ul li").removeClass('themeActive');
		$('.themeItem ul li .' + themeName).parent().addClass('themeActive');
	}

	initTheme(localStorage.getItem('superTheme'));
	$.get("sys/perm",function (data) {
		console.log(data);
		for (var i=0;i<data.length;i++){
			var menulist="<ul>";
			$.each(data[i].children,function (key,val) {
				menulist +="<li data-url='";
				if (val.attributes){
					menulist+=val.attributes["url"];
				}
				menulist+="'><i class='";
				menulist+=val.iconCls;
				menulist+="' ></i>";
				menulist+=val.text;
				menulist+="</li>";
			});
			menulist+="</ul>";
			$('#main-menu').accordion('add', {
				title: data[i]['text'],
				iconCls: data[i]['iconCls'],
				content: menulist,
				selected: false
			});
		}
		// 左侧导航分类选中样式
		$(".panel-body .accordion-body>ul").on('click', 'li', function() {
			$(this).siblings().removeClass('super-accordion-selected');
			$(this).addClass('super-accordion-selected');

			//新增一个选项卡
			var tabUrl = $(this).data('url');
			var tabTitle = $(this).text();
			var icon="";
			if (this.children[0].className){
				icon=this.children[0].className;
			}
			//tab是否存在
			if($("#tt").tabs('exists', tabTitle)) {
				$("#tt").tabs('select', tabTitle);
			} else {
				var content = '<iframe scrolling="auto" frameborder="0"  src="' + tabUrl + '" style="width:100%;height:99%;"></iframe>';
				$('#tt').tabs('add', {
					title: tabTitle,
					content: content,
					// href: tabUrl,
					iconCls: icon,
					closable: true
				});
			}
		});
	})


	// 设置按钮的下拉菜单
	$('.super-setting-icon').on('click', function() {
		$('#mm').menu('show', {
			top: 50,
			left: document.body.scrollWidth - 160
		});
	});

	// 修改主题
	$('#themeSetting').on('click', function() {
		var themeWin = $('#win').dialog({
			width: 460,
			height: 260,
			modal: true,
			title: '主题设置',
			buttons: [{
				text: '保存',
				id: 'btn-sure',
				handler: function() {
					themeWin.panel('close');
					// css
					var themeName = $(".themeItem ul li.themeActive>div").attr('class');
					initTheme(themeName);
					localStorage.setItem('superTheme', themeName);
				}
			}, {
				text: '关闭',
				handler: function() {
					themeWin.panel('close');
				}
			}],
			onOpen: function() {
				$(".themeItem").show();
			}
		});
	});

	// 勾选主题
	$(".themeItem ul li").on('click', function() {
		$(".themeItem ul li").removeClass('themeActive');
		$(this).addClass('themeActive');
	});

	// 退出系统
	$("#logout").on('click', function() {
		$.messager.confirm('提示', '确定退出系统？', function(r) {
			if(r) {
				window.location.href="/logout";
			}
		});
	});
});
// function openPasswordModifyDialog(){
// 	$("#dlg").dialog("open").dialog("setTitle","修改密码");
//
// }
//修改密码
// function modifyPassword(){
// 		$("#fm").form("submit",{
// 			url:"/sys/user/modifyPassword",
// 			onSubmit:function(){
// 				var newPassword=$("#newPassword").val();
// 				var newPassword2=$("#newPassword2").val();
// 				if(!$(this).form("validate")){
// 					return false;
// 				}
// 				if(newPassword!=newPassword2){
// 					$.messager.alert("系统提示","确认新密码输入错误！");
// 					return false;
// 				}
// 				return true;
// 			},
// 			success:function(result){
// 				var result=eval('('+result+')');
// 				if(result.success){
// 					$.messager.alert("系统提示","密码修改成功，下一次登录生效！");
// 					resetValue();
// 					$("#dlg").dialog("close");
// 				}else{
// 					$.messager.alert("系统提示","密码修改失败，请联系管理员！");
// 				}
// 			}
// 		});
//
//
// }
// function resetValue(){
// 	$("#newPassword").val("");
// 	$("#newPassword2").val("");
// }